package com.authine.cloudpivot.ext.applicationservice;

import com.authine.cloudpivot.ext.dto.InquiryGoodParam;
import com.authine.cloudpivot.ext.dto.PageObject;
import com.authine.cloudpivot.ext.dto.QuotationGoodDto;
import com.authine.cloudpivot.ext.utils.BoServiceUtils;
import com.authine.cloudpivot.ext.utils.JdbcUtil;
import com.authine.hermes.app.launcher.service.ApplicationService;
import com.authine.mvp.app.launcher.dto.bo.query.ListBOQuery;
import com.authine.mvp.app.launcher.dto.bo.response.BOList;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.authine.hermes.app.launcher.service.ApplicationServiceFoundation.CUSTOM_SERVICE;
@Slf4j
@Path("QuotationGoodService")
public class QuotationGoodService extends ApplicationService {

    @Path("queryQuotationGoodService")
    @POST
    @Operation(summary = "询价",tags = CUSTOM_SERVICE)
    public com.alibaba.cola.dto.SingleResponse<BOList> queryQuotationGoodService(@Parameter QuotationGoodDto good){

        PageObject<Map<String,Object>> pageObject = new PageObject<Map<String,Object>>();
        List<Map<String, Object>> orderList = new ArrayList<>();
        int count = 0;
        try{
            Integer pageCurrent = good.getPageCurrent() == null ?  1:good.getPageCurrent();
            Integer pageSize = good.getPageSize() == null ? 10 :good.getPageSize();

            // 设置当前页
            good.setPageCurrent(pageCurrent);
            // 设置每页显示的条数
            pageObject.setPageSize(pageSize);
            // 设置总记录数

            count = JdbcUtil.getQuotationGoodCountSql(good);
            log.info("总条数:{}"+count);
            pageObject.setRowCount(count);
            pageObject.setPageCount(0);
            if (count != 0 ){
                // 设置总页数
                int pageNum = count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1;
                pageObject.setPageCount(pageNum);
                // 设置每页显示数据集合
                // 开始的记录数
                int start = (pageCurrent - 1) * pageSize;
                System.out.println(start+"-"+pageSize);
                orderList = JdbcUtil.getQuotationGoodList(good, start, pageSize);

            }else{
                pageObject.setPageCount(0);
                pageObject.setData(null);
            }
        }catch(Exception e){
            log.info("查询出错",e);
        }
        BOList bolist = new BOList();
        bolist.setData(orderList);
        bolist.setTotal(count);
        bolist.setSchemaCode("toBudget");
        return com.alibaba.cola.dto.SingleResponse.of(bolist);

    }

    @Path("queryClassifyNote")
    @POST
    @Operation(summary = "根据询价商品id获取澄清记录",tags = CUSTOM_SERVICE)
    public com.alibaba.cola.dto.SingleResponse<BOList> queryClassifyNote(@Parameter InquiryGoodParam inquiryGoodParam){
        com.alibaba.cola.dto.SingleResponse<BOList> classifyNotes = BoServiceUtils.findListBySimple(
                "t_inquiry_goods_clarifys", "inquiry_goods_id",
                inquiryGoodParam.getInquiry_goods_id());
        return  classifyNotes;

    }




    @Path("queryQuotedPrice")
    @POST
    @Operation(summary = "密封报价",tags = CUSTOM_SERVICE)
    public com.alibaba.cola.dto.SingleResponse<BOList> queryQuotedPrice(@Parameter ListBOQuery baseQueryDto) {
        com.alibaba.cola.dto.SingleResponse<BOList> list = listBO(baseQueryDto);

        if (!list.isSuccess()){
            return list;
        }else{
            List<Map<String, Object>> data = list.getData().getData();
            if (data != null  && data.size() > 0 ){
                data.forEach(item->{
                    String inquiry_goods_status = (String) item.get("inquiry_goods_status");
                    Integer picketaged = (Integer) item.get("picketaged");
                    if (StringUtils.equals("5",inquiry_goods_status)){
                        item.put("closeType","手动关闭");
                    }else if (StringUtils.equals("4",inquiry_goods_status) || picketaged != null && picketaged == 1){
                        item.put("closeType","已定标");
                    }else{
                        item.put("close","询价截止");
                    }
                    Integer quote_count = (Integer)item.get("quote_count")== null ? 0:(Integer)item.get("quote_count");
                    Integer partake_count = (Integer)item.get("partake_count")== null ? 0:(Integer)item.get("partake_count");
                    StringBuffer sb = new StringBuffer();
                    sb.append(quote_count + "/" +partake_count);
                    item.put("quote_partake_count",sb.toString());
                });
            }
        }
        return list;
    }
}
